using System.Xml;
using MessageGateway;

namespace Tee
{
    /* A messaging component which forwards incoming message to two output channels.
     * See http://www.eaipatterns.com/WireTap.html
     */
    public class TeeComponent
	{
	    IMessageReceiver input;
        IMessageSender output1;
        IMessageSender output2;

        public TeeComponent(string inputQueue, string outputQueue1, string outputQueue2, IMessageGatewayFactory factory)
        {
            input = factory.GetReceiverInstance(inputQueue);
            output1 = factory.GetSenderInstance(outputQueue1);
            output2 = factory.GetSenderInstance(outputQueue2);
            input.AddEventHandler(new OnMsgEvent(OnMessage));
            input.Activate();
        }

        public void AddEventHandler(OnMsgEvent handler)
        {
            input.AddEventHandler(handler);
        }

        protected void OnMessage(XmlDocument message)
        {
            output1.Send(message);
            output2.Send(message);
        }

    }
}
